package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Role;
import com.qk.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
public class RoleController {
    @Autowired
    private RoleService roleService;

    //角色列表查询
    @GetMapping("/roles")
    public Result list(String name,String  label,
                       @RequestParam(defaultValue = "1") Integer page,//@RequestParam
                       @RequestParam(defaultValue = "10")  Integer pageSize) {
        log.info("列表查询    " + name + "  " + label + "  " + page + "  " + pageSize);
        PageResult<Role> pageResult = roleService.list(name, label, page, pageSize);
        return Result.success(pageResult);
    }
    //删除角色
    @Log
    @DeleteMapping("/roles/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除角色  " + id);
        roleService.delete(id);
        return Result.success();
    }

    //添加角色
    @Log
    @PostMapping("/roles")
    public Result add(@RequestBody Role role) {
        log.info("添加角色  " + role);
        roleService.add(role);
        return Result.success();
    }

    //根据Id查询
    @GetMapping("/roles/{id}")
    public Result get(@PathVariable Integer id) {
        log.info("查询角色  " + id);
        Role role = roleService.get(id);
        return Result.success(role);
    }

    //修改角色
    @Log
    @PutMapping("/roles")
    public Result update(@RequestBody Role role) {
        log.info("修改角色  " + role);
        roleService.update(role);
        return Result.success();
    }

    //询所有角色(直接返回即可)查
    @GetMapping("/roles/list")
    //之前这里出了一点小问题,要严格按照接口文档要求来写!!!!
    //之前这里出了一点小问题,要严格按照接口文档要求来写!!!!
    // 之前这里出了一点小问题,要严格按照接口文档要求来写!!!!
    public Result listAll() {
        log.info("查询所有角色");
        return Result.success(roleService.listAll2());
    }
}
